package elevatronic.view.simulation;

import java.awt.Graphics;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JPanel;

import elevatronic.view.ImageView;


/**
 * 
 * @author Benjamin CORNEC
 * the class permit to draw image in the window
 * it's the place where we can see the simulation run
 */
public class SimulationPanel extends JPanel {
	private static final long serialVersionUID = -3673324534001160635L;
	
	//contain all images of the application
	private List<ImageView> images;	
	synchronized public List<ImageView> getImages() {
		return images;
	}
	synchronized public void setImages(List<ImageView> _images) {
		this.images = _images;
	}

	
	//the building contain every picture of the simulation
	private BuildingView buildingView;
	public BuildingView getBuildingView() {
		return buildingView;
	}
	public void setBuildingView(BuildingView buildingView) {
		this.buildingView = buildingView;
	}


	//set the main picture
	public SimulationPanel(BuildingView buildingView,int wdth,int hght){
		setSize(wdth,hght);
		setVisible(true);		
		setBuildingView(buildingView);
		setImages(new ArrayList<ImageView>());		
	}
	
	//draw again the picture at each update
	public void update(int step){
		//check if the building's image is in the list and update it
		getBuildingView().update(step);
		if (!getImages().contains(getBuildingView().getImageBuilding())){
			getImages().add(getBuildingView().getImageBuilding());
		}


		//check if all floor's image are in the list and update them
		for(FloorView floorView:getBuildingView().getFloorsView()){
			floorView.update(step);
			if (!getImages().contains(floorView.getImageFloor())){
				getImages().add(floorView.getImageFloor());
			}
		}

		//check if all elevators's image are in the list and update them
		for(ElevatorView elevatorView:getBuildingView().getElevatorsView()){
			elevatorView.update(step);
			if (!getImages().contains(elevatorView.getImageElevator())){
				getImages().add(elevatorView.getImageElevator());
			}
		}		
		
		//check if all people's image are in the list and update them
		for(PeopleView peopleView:getBuildingView().getPeopleView()){
			//if the people is deleted, we delete also the image
			if (peopleView.getPeople()==null){
				getBuildingView().getPeopleView().remove(peopleView);
				peopleView=null;
			} else {
				//if the people is still alive, we check and update
				peopleView.update(step);
				if (!getImages().contains(peopleView.getImagePeople())){
					getImages().add(peopleView.getImagePeople());
				}
			}
		}
		
		//launch paintComponent(Graphics g)  
		this.repaint();
	}
	
	
	//this method draw and refresh all pictures
	public void paintComponent(Graphics g)  
	{
		super.paintComponent(g);
		
		for(ImageView image:getImages()){
			if (image.isVisible()){
				g.drawImage(image.getImage(),image.getPosX(),image.getPosY(),image.getWdth(),image.getHght(),null);
			}
		}
	}
	
	
}
